package cn.org.eshow.dao.hibernate;

import cn.org.eshow.bean.query.WithdrawQuery;
import cn.org.eshow.common.dao.EnhancedRule;
import cn.org.eshow.common.page.Page;
import cn.org.eshow.dao.WithdrawDao;
import cn.org.eshow.model.Withdraw;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("withdrawDao")
public class WithdrawDaoHibernate extends GenericDaoHibernate<Withdraw, Integer> implements WithdrawDao {

    public WithdrawDaoHibernate() {
        super(Withdraw.class);
    }

    @Override
    public List<Withdraw> list(WithdrawQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return list(rule);
    }

    @Override
    public Page<Withdraw> search(WithdrawQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        if (query != null) {
            rule.setPageSize(query.getPagesize());
            rule.setOffset(query.getOffset());
        }
        return page(rule);
    }

    @Override
    public Long count(WithdrawQuery query) {
        EnhancedRule rule = new EnhancedRule();
        setRule(rule, query);
        return count(Withdraw.class, rule);
    }

    public EnhancedRule setRule(EnhancedRule rule, WithdrawQuery query) {
        if (query != null) {
            if (query.id != null)
                rule.add(Restrictions.eq("id", query.id));
            if (query.startAddTime != null)
                rule.add(Restrictions.ge("addTime", query.startAddTime));
            if (query.endAddTime != null)
                rule.add(Restrictions.le("addTime", query.endAddTime));
            if (query.no != null) {
                rule.add(Restrictions.like("no", query.no, MatchMode.ANYWHERE));
            }
            if (!StringUtils.isEmpty(query.way)) {
                rule.add(Restrictions.eq("way", query.way));
            }
            if (query.price != null) {
                rule.add(Restrictions.eq("price", query.price));
            }
            if (query.channel != null) {
                rule.add(Restrictions.like("channel", query.channel, MatchMode.ANYWHERE));
            }
            if (query.name != null) {
                rule.add(Restrictions.like("name", query.name, MatchMode.ANYWHERE));
            }
            if (query.account != null) {
                rule.add(Restrictions.like("account", query.account, MatchMode.ANYWHERE));
            }
            if (query.state != null) {
                rule.add(Restrictions.eq("state", query.state));
            }
            if (query.content != null) {
                rule.add(Restrictions.like("content", query.content, MatchMode.ANYWHERE));
            }
            if (query.attachment != null) {
                rule.add(Restrictions.like("attachment", query.attachment, MatchMode.ANYWHERE));
            }
            if (query.remark != null) {
                rule.add(Restrictions.like("remark", query.remark, MatchMode.ANYWHERE));
            }
            if (query.userId != null) {
                rule.add(Restrictions.eq("user.id", query.userId));
            }
            if (query.accountantUserId != null) {
                rule.add(Restrictions.eq("accountantUser.id", query.accountantUserId));
            }
            if (!StringUtils.isEmpty(query.getOrder())) {
                rule.addOrder(query.getDesc() ? Order.desc(query.getOrder()) : Order.asc(query.getOrder()));
            }
        }
        return rule;
    }
}
